#!/bin/bash
sh_pwd=`pwd`

HADOOP_HOME=/usr/local/hadoop/hadoop-3.3.0 
echo export PATH=$PATH:$HADOOP_HOME/bin 
echo export PATH=$PATH:$HADOOP_HOME/sbin 


echo export JAVA_HOME=/usr/local/jdk8>>/etc/profile
echo export JRE_HOME=/usr/local/jdk8/jre>>/etc/profile
echo export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH>>/etc/profile
echo export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$JAVA_HOME:$PATH>>/etc/profile
sleep 1
source /etc/profile

systemctl stop firewalld.service
systemctl disable firewalld.service
if [ ! -f hadoop*gz ];then
    wget https://downloads.apache.org/hadoop/common/hadoop-3.3.0/hadoop-3.3.0.tar.gz 
fi
rm /usr/local/hadoop -rf
mkdir -p /usr/local/hadoop
tar -zxf hadoop-3.3.0.tar.gz -C /usr/local/hadoop
cd /usr/local/hadoop
mkdir tmp 
mkdir var 
mkdir dfs 
mkdir dfs/name 
mkdir dfs/data
cd /usr/local/hadoop/hadoop-3.3.0/etc/hadoop/
rm workers
cp $sh_pwd/workers ./workers

echo export JAVA_HOME=/usr/local/jdk8>>./hadoop-env.sh

temp=$(cat /etc/hostname)
temp=${temp%.*}
echo $temp #which node excute this bash
com=hdfs://master:9000
cd /usr/local/hadoop/hadoop-3.3.0/etc/hadoop/
sed -i  "/<configuration>/r /$sh_pwd/core" core-site.xml
sed -i "s;<value>.*<\/value>;<value>$com<\/value>;g" core-site.xml

sed -i  "/<configuration>/r /$sh_pwd/hdfs-site" hdfs-site.xml
sed -i  "/<configuration>/r /$sh_pwd/mapred-site" mapred-site.xml
sed -i  "/<configuration>/r /$sh_pwd/yarn" yarn-site.xml

cd /usr/local/hadoop/hadoop-3.3.0/sbin
sed -i "1 r /$sh_pwd/temp4" start-dfs.sh
sed -i "1 r /$sh_pwd/temp4" stop-dfs.sh
sed -i "1 r /$sh_pwd/temp5" start-yarn.sh
sed -i "1 r /$sh_pwd/temp5" stop-yarn.sh 
cd /usr/local/hadoop/hadoop-3.3.0/bin

if [ $(cat /etc/hostname) = "master" ];then
echo yes
$sh_pwd/xsync /usr/local/hadoop/
./hadoop namenode -format
fi

cd /usr/local/hadoop/hadoop-3.3.0/sbin
if [ $(cat /etc/hostname) = "master" ];then
./start-dfs.sh
fi

if [ '$(cat /etc/hostname)' = "node1" ];then
./start-yarn.sh
fi

